package com.david.zhufengfm.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.Log;

import com.david.zhufengfm.entity.album.AlbumTrackDetail;
import com.david.zhufengfm.entity.album.PlayTrack;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by David on 2015/10/24.
 * qq:978944820
 */
public class MediaService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
    private final IBinder mBinder = new PlayControlBinder();

    private MediaPlayer mediaPlayer;
    private ArrayList<PlayTrack> tracks;
    private long currentTrackId;
    private int trackIndex;
    private boolean isPause;
    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer=new MediaPlayer();
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnCompletionListener(this);
        tracks=new ArrayList<>();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        tracks=intent.getParcelableArrayListExtra("tracks");
        currentTrackId=intent.getLongExtra("currentTrackId",0);
//        tracks= Arrays.asList(trackses);
        String url=tracks.get(getCurrentPlayItem(tracks)).getPlayUrl64();
//        PlayTrack track=intent.getParcelableExtra("track");
//        String url=track.getPlayUrl64();
        Log.i("playUrl",url);
        if (url != null) {
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return super.onStartCommand(intent, flags, startId);
//        return 1;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    public int getCurrentPlayItem(  List<PlayTrack>tracks){

        int trackIndex=0;
        for (int i = 0; i < tracks.size(); i++) {
            if (tracks.get(i).getTrackId() == currentTrackId) {
                trackIndex=i;

            }
        }
        return trackIndex;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mediaPlayer.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.reset();
        try {
            trackIndex++;

            mp.setDataSource(tracks.get(trackIndex).getPlayPathAacv164());
            mp.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class PlayControlBinder extends Binder {
        public void play(){
            if (!mediaPlayer.isPlaying()) {
                try {
                    if (isPause) {
                        mediaPlayer.start();
                    }else{
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }else{
                pause();
            }
        }
        public void playNew(String url){
            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource("");
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        public void pause(){
            if (mediaPlayer.isPlaying()) {
                isPause=true;
                mediaPlayer.pause();
            }
        }

        public void stop(){
            if (mediaPlayer != null) {
                isPause=false;
                mediaPlayer.stop();
            }
        }

        public void nextLong(){
            if (currentTrackId<tracks.size()) {
                trackIndex++;
            }else{
                trackIndex=0;
            }
        }



        public void seekTo(int progress){
            mediaPlayer.seekTo(progress);
        }
    }

}
